import { normalizePath } from '@rollup/pluginutils'
import { join, parse, relative, resolve, sep } from 'path'
const PLATFORMS = new Set(['taro', 'h5', 'weapp'])
export const getRelativePath = function (filePath: string) {
    const newPath = normalizePath(filePath)
    return relative(resolve(process.cwd()), newPath)
}
export const getComponentName = function (path: string) {
    if (typeof path !== 'string') return ''

    const parsed = parse(path)
    const pathWithoutExt = join(parsed.dir, parsed.name)
    const pathParts = pathWithoutExt.split(sep).filter((part) => part)
    if (pathParts.length === 0) return ''
    while (pathParts.length > 0 && pathParts.at(-1) === 'index') {
        pathParts.pop()
    }
    while (pathParts.length > 0 && PLATFORMS.has(pathParts.at(-1) || '')) {
        pathParts.pop()
    }
    return pathParts.at(-1) ?? ''
}
